package game;

import algos.Action;
import algos.ContextStack;
import algos.GameController;

import game.actions.PlaceWordAction;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import reader.DicoAccessor;
import root.GridOfWords;
import root.Word;

/**
 *
 * @author nicolas
 */
public class CrosswordGameController extends GameController<GridOfWords> {
	
	private GridOfWords mLastContext;

	@Override
	public boolean isComplete(ContextStack<GridOfWords> contextStack) {
		return contextStack.currentContext().isFull();
	}

	@Override
	public List<Action<GridOfWords>> getActions(GridOfWords context) {
		List<Action<GridOfWords>> actions = new ArrayList<Action<GridOfWords>>();
		Word w = context.getCurrentWord();
		if(w != null)
		{
			String pattern = w.createPattern();
			List<String> possibilities =  DicoAccessor.DICO_HMM.getMatchPattern(pattern);
			for (String p : possibilities) {
				actions.add(new PlaceWordAction(w, p));
			}
		}
		return actions;
	}
	
	@Override
	public boolean isCorrect(GridOfWords context) {		
		boolean isCorrect = true;
		
		List<Word> words = context.getWords();
		for(int i=0; i<words.size() && isCorrect == true; i++)
		{
			if(!context.equals(mLastContext))
				words.get(i).onLettersChanged(context);
			
			isCorrect &= DicoAccessor.DICO_HMM.hasAtLeastOnMatch(words.get(i).createPattern());
		}
		
		mLastContext = context;
		
		return isCorrect;
	}
	
	
}
